from socket import *
import sys


def open_port(ip,*port):
    target_ip = ip
    open_ports=[]
    for i in port:
        s = socket(AF_INET, SOCK_STREAM)
        s.settimeout(2)
        result = s.connect_ex((target_ip, i))
        if(result == 0):
            open_ports.append(i)
        try:
            s.close()
        except:
            pass
    return open_ports




def scan1(ip):
        ports=port.open_port(ip)
        res=[]
        if ports==False :
                res.append("#".join((ip,"0","0","0","0") ))
        else:
                try:

                        tip="http://"+ip

                        req1 = urllib2.Request(url = tip,headers = headers)
                        r1=urllib2.urlopen(req1,timeout=2)
                        url1=r1.geturl()
                        res.append("#".join((ip,url1,"0","80","1") ) )
                        print tip
                except:
                        res.append("#".join( (ip,"0","0","80","0")) )

                try:
                        dm=os.popen("dig -x "+ip+" +short").readlines()
                        tdm="http://"+ dm[0].strip("\n")[:len(dm[0])-1]
                        req2 = urllib2.Request(url = tdm,headers = headers)
                        r2=urllib2.urlopen(req2,timeout=2)
                        url2=r2.geturl()
                        res.append("#".join( (ip,url2,"1","80","1")) )
                except:
                        res.append("#".join( ( ip,"0","1","80","0")) )


if __name__ == '__main__':
    t=sys.argv[1]
    r=open_port(t,80,443)
    print r
